<?php

namespace App\Providers;
use App\Models\Company\CompanyUser;
use App\Models\Personal\PersonalUser;
use Illuminate\Support\ServiceProvider;
use Validator,CustomPassword;

class MobileServiceProvider extends ServiceProvider{
    public function boot(){
        //手机存在时
        Validator::extend('mobile', function($attribute, $value, $parameters) {
            if (preg_match("/^1[3-9]{1}[0-9]{9}$/Uis", $value)) {
                return false;
            }else{
                return true;
            }
        });


        //自定义电话号码验证规则改写
        Validator::extend('check_phone', function($attribute, $value, $parameters) {
            $phone =  $parameters[0] . '-' . $value . '-' . $parameters[1];
            $phone = trim($phone,'-');
            if (preg_match("/(^[0-9]{2,5}\-[0-9]{7,9})((\-\d{0,10})?)$/Uis", $phone)) {
                return true;
            }else{
                return false;
            }
        });

        //自定义电话号码验证规则改写
        Validator::extend('check_mobile_phone', function($attribute, $value, $parameters) {
            $phone = trim($value,'-');
            if (preg_match("/(^[0-9]{2,5}\-[0-9]{7,9})((\-\d{0,10})?)$/Uis", $phone)) {
                return true;
            }else{
                return false;
            }
        });

        //自定义企业电话号码验证规则改写
        Validator::extend('check_company_phone', function($attribute, $value, $parameters) {
            if (preg_match("/(^[0-9]{2,5}\-[0-9]{7,9})((\-\d{0,10})?)$/Uis", $value)) {
                return true;
            }else{
                return false;
            }
        });

        //自定义传真号码验证规则改写
        Validator::extend('check_fax', function($attribute, $value, $parameters) {
            $fax = trim($value,'-');
            if (preg_match("/(^[0-9]{2,5}\-[0-9]{7,9})$/Uis", $fax)) {
                return true;
            }else{
                return false;
            }
        });

        //自定义qq号码验证规则改写
        Validator::extend('check_qq', function($attribute, $value, $parameters) {
            $qq = trim($value,'');
            if (preg_match("/(^[1-9]{1}[0-9]{4,15})$/Uis", $qq)) {
                return true;
            }else{
                return false;
            }
        });

        //自定义身高验证规则改写
        Validator::extend('check_height', function($attribute, $value, $parameters) {
            $height = trim($value,'');
            if (preg_match("/(^[0-9]{2,3})$/Uis", $height)) {
                return true;
            }else{
                return false;
            }
        });

        //自定义身高验证规则改写
        Validator::extend('check_time', function($attribute, $value, $parameters) {
            $endtime = $parameters[0];
            if(empty($value)){
                return false;
            }else if ($value <= $endtime || $endtime == '0000-00-00') {
                return true;
            }else{
                return false;
            }
        });

        //自定义密码验证规则改写
        Validator::extend('check_oldpass', function($attribute, $value, $parameters) {
            //当前用户ID
            $uid = $parameters[0];
			$password = PersonalUser::where('uid', $uid)->first(['password'])->password;
			//判断旧密码是不是正确
            return (CustomPassword::check($value,$password)) ? true : false;
        });

        //自定义密码验证规则改写
        Validator::extend('check_comoldpass', function($attribute, $value, $parameters) {
            //当前用户ID
            $uid = $parameters[0];
			$password = CompanyUser::where('uid', $uid)->first(['password'])->password;
			//判断旧密码是不是正确
            return (CustomPassword::check($value,$password)) ? true : false;
        });


        //自定义身高验证规则改写
        Validator::extend('check_salary', function($attribute, $value, $parameters) {
            $salary_below = $parameters[0];
            if ($value <= $salary_below) {
                return true;
            }else{
                return false;
            }
        });
        //自定义身高验证规则改写
        Validator::extend('check_age', function($attribute, $value, $parameters) {
            $age_below = $parameters[0];
            if ($value <= $age_below) {
                return true;
            }else{
                return false;
            }
        });

        //自定义手机验证规则改写
        Validator::extend('mobile_verification', function($attribute, $value, $parameters) {
            if (preg_match("/^1[3-9]{1}[0-9]{9}$/Uis", $value)) {
                return true;
            }else{
                return false;
            }
        });

        //短信验证码
        Validator::extend('mobile_verification_code', function($attribute, $value, $parameters) {
            $mobile = $parameters[0];
            if(session('mobile_verification') == $mobile && session('mobile_verification_code') == $value){
                return true;
            }else{
                return false;
            }
        });
        //短信验证码是不是为空
        Validator::extend('check_empty_mobile', function($attribute, $value, $parameters) {
            $mob_code = $parameters[0];
            if(!empty($value) && strlen($mob_code)>0){
                return true;
            }else{
                return false;
            }
        });

        //用户名验证(字母,中文开头,字母,中文,下划线,数字结尾)
        Validator::extend('api_check_username', function($attribute, $value, $parameters) {
            return preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z][\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u", $value);
        });

        /**
         * 验证兼职空闲时间是否存在
         * 只要存在一个即返回 true，否则返回false
         */
        Validator::extend('free_time_check', function($attribute, $value, $parameters) {
            foreach ($value as $v) {
                if ($v == 1) {
                    return true;
                }
            }
            return false;
        });
    }



    /**
     * Register the application services.
     *
     * @return void
     */

    public function register(){

    }
}
